# Maintainer: Gene Harvey <gharveymn@gmail.com>

_realname=sundials
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.0.0
pkgrel=1
pkgdesc="SUite of Nonlinear and DIfferential/ALgebraic equation Solvers (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url="https://computation.llnl.gov/projects/sundials"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-openmp"
         "${MINGW_PACKAGE_PREFIX}-petsc"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-superlu_dist")
makedepends=('make'
             'patch'
             'sed'
             "${MINGW_PACKAGE_PREFIX}-cmake>=3.12.0"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-petsc-build")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: for running examples")
conflicts=("${MINGW_PACKAGE_PREFIX}-cvode")
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/LLNL/sundials/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        '0002-sundials-missing-export.patch'
        '0003-sundials-use-default-installdirs.patch'
        '0004-sundials-superlu_dist-manual-index-size.patch'
        '0005-sundials-superlu_dist-test-link-errors.patch'
        '0006-sundials-petsc-integration.patch'
        '0007-sundials-6.0.0-superlu-support.patch')
options=('staticlibs' 'strip')
sha256sums=('c7178e54df20a9363ae3e5ac5b3ee9db756a4ddd4b8fff045127e93b73b151f4'
            '7f119fbcc8a630a4e3443e3bba252dafb4b1567ced1bf1389052253529e97ddc'
            '310b9beb86426fd2f817391baf72c0f9aefe9dc31e737daed5cc0280b7693311'
            '30800e35e46f04e0d19b245e2529e2be578043417d9bdf88c536442212cb23de'
            'af3391ca031778c238e430d3a78631842f5407806020e90529de14850addce69'
            '8ce7ab73d91eacec0701d2e35d0aa35fc514b6175d1bd57f85d750095b8b69c4'
            'b7c809c80455b6788c4a7ea08e61bdfa09c309884449a2cf70c9dd7f23316465')

prepare() {
  patch -d ${_realname}-${pkgver} -p1 < 0002-sundials-missing-export.patch
  patch -d ${_realname}-${pkgver} -p1 < 0003-sundials-use-default-installdirs.patch
  patch -d ${_realname}-${pkgver} -p1 < 0004-sundials-superlu_dist-manual-index-size.patch
  patch -d ${_realname}-${pkgver} -p1 < 0005-sundials-superlu_dist-test-link-errors.patch
  patch -d ${_realname}-${pkgver} -p1 < 0006-sundials-petsc-integration.patch
  patch -d ${_realname}-${pkgver} -p1 < 0007-sundials-6.0.0-superlu-support.patch
}

build() {
  [[ -d build-${MINGW_PACKAGE_PREFIX} ]] && rm -rf build-${MINGW_PACKAGE_PREFIX}

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})

  . ${MINGW_PREFIX}/src/petsc-*/petsc

  I_MPI_ROOT="" \
  MSYS2_ARG_CONV_EXCL="CMAKE_INSTALL_PREFIX=;EXAMPLES_INSTALL_PATH=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -D BUILD_ARKODE=ON \
    -D BUILD_CVODE=ON \
    -D BUILD_CVODES=ON \
    -D BUILD_FORTRAN_MODULE_INTERFACE=OFF \
    -D BUILD_IDA=ON \
    -D BUILD_IDAS=ON \
    -D BUILD_KINSOL=ON \
    -D BUILD_NVECTOR_MANYVECTOR=ON \
    -D BUILD_NVECTOR_MPIMANYVECTOR=ON \
    -D BUILD_NVECTOR_MPIPLUSX=ON \
    -D BUILD_NVECTOR_OPENMP=ON \
    -D BUILD_NVECTOR_PARALLEL=ON \
    -D BUILD_NVECTOR_PTHREADS=ON \
    -D BUILD_SHARED_LIBS=ON \
    -D BUILD_STATIC_LIBS=ON \
    -D BUILD_SUNLINSOL_KLU=ON \
    -D BUILD_SUNLINSOL_LAPACKBAND=ON \
    -D BUILD_SUNLINSOL_LAPACKDENSE=ON \
    -D BUILD_TESTING=ON \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -D CMAKE_RUNTIME_OUTPUT_DIRECTORY="${srcdir}/build-${MINGW_PACKAGE_PREFIX}"/build-output/bin \
    -D CMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -D ENABLE_CALIPER=OFF \
    -D ENABLE_CUDA=OFF \
    -D ENABLE_HIP=OFF \
    -D ENABLE_HYPRE=OFF \
    -D ENABLE_KLU=ON \
    -D ENABLE_LAPACK=ON \
    -D ENABLE_MAGMA=OFF \
    -D ENABLE_MPI=ON \
    -D ENABLE_OPENMP=ON \
    -D ENABLE_OPENMP_DEVICE=OFF \
    -D ENABLE_PETSC=ON \
    -D ENABLE_PTHREAD=ON \
    -D ENABLE_RAJA=OFF \
    -D ENABLE_SUPERLUDIST=ON \
    -D ENABLE_SUPERLUMT=OFF \
    -D ENABLE_SYCL=OFF \
    -D ENABLE_TRILINOS=OFF \
    -D ENABLE_XBRAID=OFF \
    -D EXAMPLES_ENABLE_C=ON \
    -D EXAMPLES_ENABLE_CXX=ON \
    -D EXAMPLES_ENABLE_CUDA=OFF \
    -D EXAMPLES_ENABLE_F2003=OFF \
    -D EXAMPLES_INSTALL=ON \
    -D EXAMPLES_INSTALL_PATH=${MINGW_PREFIX}/share/sundials/examples \
    -D KLU_LIBRARY=${MINGW_PREFIX}/lib/libklu.dll.a \
    -D LAPACK_LIBRARIES=${MINGW_PREFIX}/lib/libopenblas.dll.a \
    -D MPI_C_COMPILER=${MINGW_PREFIX}/bin/mpicc.exe \
    -D MPI_CXX_COMPILER=${MINGW_PREFIX}/bin/mpicxx.exe \
    -D MPI_Fortran_COMPILER=${MINGW_PREFIX}/bin/mpifort.exe \
    -D PETSC_INCLUDES="${PREFIX_WIN}"/src/petsc-${petsc_pkgver}/dmo/include\;"${PREFIX_WIN}"/src/petsc-${petsc_pkgver}/include \
    -D PETSC_LIBRARIES=${MINGW_PREFIX}/src/petsc-${petsc_pkgver}/dmo/lib/libpetsc.dll.a \
    -D SUNDIALS_BUILD_WITH_PROFILING=OFF \
    -D SUNDIALS_BUILD_WITH_MONITORING=ON \
    -D SUNDIALS_INDEX_SIZE=32 \
    -D SUNDIALS_PRECISION=DOUBLE \
    -D SUPERLUDIST_INCLUDE_DIR="${PREFIX_WIN}"/include/superlu_dist \
    -D SUPERLUDIST_INDEX_SIZE=32 \
    -D SUPERLUDIST_OpenMP=ON \
    -D CMAKE_C_STANDARD=11 \
    -B build-${MINGW_PACKAGE_PREFIX} \
    -S ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build build-${MINGW_PACKAGE_PREFIX}
}

check() {
  # Skip tests requiring MPI.
  ${MINGW_PREFIX}/bin/ctest \
    --exclude-regex "mpi|parallel|petsc|superludist|slunrloc" \
    --test-dir build-${MINGW_PACKAGE_PREFIX}
}

package() {
  DESTDIR="${pkgdir}" \
  ${MINGW_PREFIX}/bin/cmake --install build-${MINGW_PACKAGE_PREFIX}

  # Install the LICENSE and NOTICE files to the system licenses directory.
  install -D --mode=644  \
    --target-directory="${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname} \
    "${srcdir}/${_realname}-${pkgver}"/{LICENSE,NOTICE}

  ## Patch various files to be relocatable.

  # Patch the CMake export modules.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSTargets*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  # Patch the CMake config module.
  sed -e "s|${PREFIX_WIN}|\$\{PACKAGE_PREFIX_DIR\}|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  sed -e "s|${MINGW_PREFIX}|\$\{PACKAGE_PREFIX_DIR\}|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  # Repoint the PETSc configuration to the runtime package (rather than the -build package).
  sed -e "s|src/petsc-[^/]\+/dmo/include|include/petsc/dmo|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake
      
  sed -e "s|src/petsc-[^/]\+/dmo/lib|lib/petsc/dmo|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  sed -e "s|src/petsc-[^/]\+/include|include/petsc|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  # Fix some broken code in the config module.
  sed -e "s/if( AND NOT TARGET caliper)/if(OFF AND NOT TARGET caliper)/" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  # Patch the CMakeLists.txt files in the examples.
  shopt -s globstar
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/sundials/examples/**/CMakeLists.txt; do
    _fdir=$(dirname "${_f}")
    _relative_root=$(realpath --relative-to="${_fdir}" "${pkgdir}${MINGW_PREFIX}")
    sed -e "s|${PREFIX_WIN}|\$\{CMAKE_CURRENT_LIST_DIR\}/${_relative_root}|g"   -i "${_f}"
    sed -e "s|${MINGW_PREFIX}|\$\{CMAKE_CURRENT_LIST_DIR\}/${_relative_root}|g" -i "${_f}"
    sed -e "s|src/petsc-[^/]\+/dmo/include|include/petsc/dmo|g"                 -i "${_f}"
    sed -e "s|src/petsc-[^/]\+/dmo/lib|lib/petsc/dmo|g"                         -i "${_f}"
    sed -e "s|src/petsc-[^/]\+/include|include/petsc|g"                         -i "${_f}"
  done
}
